import {LOGIN_TYPE,LOGOUT_TYPE} from '../actions/user';
let userInfo = localStorage.getItem('userInfo')
try{
    userInfo = JSON.parse(userInfo) || {}
}catch(err){
    userInfo = {}
}

const initState = {
    userInfo
}
const reducer = function(state=initState,action){
    // state: 当前模块的state
    console.log("action",action);
    let newState = state;
    switch(action.type){
        // store.dispatch({type:'login',payload})
        case LOGIN_TYPE:
            newState = {
                userInfo:action.payload
            }
            // 更新本地存储数据
            localStorage.setItem('userInfo',JSON.stringify(action.payload))
            return newState;
        // store.dispatch({type:'logout'})
        case LOGOUT_TYPE:
            newState = {
                userInfo:{}
            }
            localStorage.removeItem('userInfo')
            return newState;
            
        default:
            return newState

    }

}

export default reducer;